package net.ck.hzq.view;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Bean.LearnMore;
import Bean.LearnQues;
import Bean.LearnQuestwo;
import Bean.Problems;
import Utils.HelloWordModel;
import Utils.ToastUtil;
import adapter.CustomGridLayoutManager;
import adapter.LearnMibbleAdapter;
import adapter.LearnMoreLeftAdapter;
import retrofit2.Call;
import retrofit2.Response;
import util.UltimateBar;


public class LearnMoreActivity extends Activity implements View.OnClickListener {

    private LearnMibbleAdapter learnMibbleAdapter;

    private LearnMoreLeftAdapter learnMoreLeftAdapter;

    //相关问题集合
    private List<Problems> xgques;
    //更多问题集合
    private List<LearnQuestwo> moreques = new ArrayList<>();
    List<Problems> newproblem = new ArrayList<>();
    //向弹出层传递RelativeLayout用的
    private RelativeLayout repop;

    private RelativeLayout head_layout;

    private EditText learn_input;

    private TextView linkf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //头部导航栏颜色，就是时间网络那一栏
        UltimateBar ultimateBar = new UltimateBar(this);
        ultimateBar.setColorBar(Color.parseColor("#000000"), false);
        setContentView(R.layout.activity_readmore);
        repop = (RelativeLayout) findViewById(R.id.relaall);
        head_layout = (RelativeLayout) findViewById(R.id.head_layout);
        learn_input = (EditText) findViewById(R.id.learn_input);
        linkf = (TextView) findViewById(R.id.linkf);
        head_layout.setOnClickListener(this);
        linkf.setOnClickListener(this);

        //相关问题
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.learn_recycler_view);
        CustomGridLayoutManager linearLayoutManagerxg = new CustomGridLayoutManager(LearnMoreActivity.this);
        recyclerView.setLayoutManager(linearLayoutManagerxg);
        linearLayoutManagerxg.setScrollEnabled(false);
        recyclerView.setHasFixedSize(true);
        learnMibbleAdapter = new LearnMibbleAdapter(LearnMoreActivity.this);
        recyclerView.setAdapter(learnMibbleAdapter);

        //头布局header
//        View headerView =View.inflate(this,R.layout.item_learnheader,null);
//        RelativeLayout relaheader = (RelativeLayout) headerView.findViewById(R.id.header_reaall);
//        底布局bottom
//        View bottomView = View.inflate(this,R.layout.item_learnfooter,null);
//        RelativeLayout relafooter = (RelativeLayout)  bottomView.findViewById(R.id.bootmserv);
        //更多问题左边布局
//        learnMoreLeftAdapter = new LearnMoreLeftAdapter(LearnMoreActivity.this);
//        headerAndFooterWrapper = new HeaderViewAdapter(learnMoreLeftAdapter);
//        headerAndFooterWrapper.addHeaderView(relaheader);
//        headerAndFooterWrapper.addFooterView(relafooter);

        RecyclerView recyclerView1 = (RecyclerView) findViewById(R.id.learn_moreleftrecycler_view);
        CustomGridLayoutManager linearLayoutManager = new CustomGridLayoutManager(LearnMoreActivity.this);
        recyclerView1.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setScrollEnabled(false);
        recyclerView1.setHasFixedSize(true);
        learnMoreLeftAdapter = new LearnMoreLeftAdapter(LearnMoreActivity.this);
        recyclerView1.setAdapter(learnMoreLeftAdapter);

//        recyclerView1.setAdapter(headerAndFooterWrapper);
        getNews();

        learn_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                newproblem.clear();
                for (int i = 0;i<xgques.size();i++){
                    if(xgques.get(i).getTitle().contains(s.toString())||s.toString().equals("")||xgques.get(i).getContent().contains(s.toString())){
                        newproblem.add(xgques.get(i));
                    }
                }
                learnMibbleAdapter.setupData(newproblem,s.toString());
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.learn_input:
                Toast.makeText(this, "暂未开放", Toast.LENGTH_SHORT).show();
                break;
            case R.id.head_layout:
                finish();
                break;
            case R.id.linkf:
//                Intent intent = new Intent(LearnMoreActivity.this, KF5CustomerAct.class);
//                startActivity(intent);
//                Toast.makeText(this, "暂未开放", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private List<Problems> getProleZero() {
        List<Problems> problemsList = new ArrayList<>();
        Problems problems2 = new Problems();
        problems2.setTitle("");
        problems2.setContent("");
        problemsList.add(problems2);
        Problems problems3 = new Problems();
        problems3.setTitle("");
        problems3.setContent("");
        problemsList.add(problems3);
//        Problems problems4 =new Problems();
//        problems4.setTitle("要死要死的标题4");
//        problems4.setContent("完蛋完蛋的答案完蛋完蛋的答案完蛋完蛋的答案完蛋完蛋的答案完蛋完蛋的答案完蛋完蛋的答案完蛋完蛋的答案");
//        problemsList.add(problems4);
//        problemsList.add(problems4);
//        problemsList.add(problems4);
//        problemsList.add(problems4);
//        problemsList.add(problems4);
        return problemsList;
    }

    private List<Problems> getProFirst() {
        List<Problems> problemsList = new ArrayList<>();
        Problems problems2 = new Problems();
        problems2.setTitle("");
        problems2.setContent("");
        problemsList.add(problems2);
        return problemsList;
    }

    public void getNews() {
        Call<LearnMore> userMsgCall = HelloWordModel.getInstance(LearnMoreActivity.this).getLearnInfor();
        userMsgCall.enqueue(new retrofit2.Callback<LearnMore>() {
            @Override
            public void onResponse(@NonNull Call<LearnMore> call, @NonNull
                    Response<LearnMore> response) {
                try{
                LearnMore body = response.body();
                if (body != null) {
                    List<LearnQues> dataBeans = body.getData();
                    if (body.getMsg().equals("success") && body.getData() != null) {
                        for (int i = 0; i < dataBeans.size(); i++) {
                            if (dataBeans.get(i).getGroup().equals("相关问题")) {
                                xgques = dataBeans.get(i).getProblems();
                                learnMibbleAdapter.setupData(xgques);
                            } else {
                                LearnQuestwo learnQuestwo = new LearnQuestwo();
                                learnQuestwo.setGroup(dataBeans.get(i).getGroup());
                                learnQuestwo.setStatus(false);
                                if (dataBeans.get(i).getProblems().size() == 0) {
//                                    LogUtils.d("--------来源总长度-----------"+getProleZero().size());
                                    learnQuestwo.setProblems(getProleZero());
                                } else if (dataBeans.get(i).getProblems().size() == 1) {
                                    learnQuestwo.setProblems(getProFirst());
                                } else {
                                    learnQuestwo.setProblems(dataBeans.get(i).getProblems());
                                }
                                moreques.add(learnQuestwo);
                            }
                        }
                        learnMoreLeftAdapter.setupData(moreques, repop);
                    }
                }
            }catch (Exception e){
                ToastUtil.show(LearnMoreActivity.this,e.toString());
            }
            }

            @Override
            public void onFailure(Call<LearnMore> call, Throwable t) {
                Log.d("test", t.toString());

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
